package cn.wolfcode.car.business.web.controller.system;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.query.BpmnInfoQueryObject;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.util.file.FileUploadUtils;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Multipart;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 结算单列表控制器
 business.web.controller.system.BpmnInfoController
 */
@Controller
@RequestMapping("system/bpmnInfo")
public class BpmnInforController {
    //模板前缀
    private static final String prefix = "business/system/bpmnInfo/";

    @Autowired
    private IBpmnInfoService bpmnInfoService;

    @Autowired
    private IUserService userService;


    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:view")
    @RequestMapping("/listPage")
    public String listPage() {
        return prefix + "list";
    }

    @RequiresPermissions("system:bpmnInfo:add")
    @RequestMapping("/addPage")
    public String addPage() {
        return prefix + "add";
    }


    @RequiresPermissions("system:bpmnInfo:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        model.addAttribute("bpmnInfo", bpmnInfoService.get(id));
        return prefix + "edit";
    }

    //请求到上传部署文件页面
    @RequiresPermissions("system:bpmnInfo:deployPage")
    @RequestMapping("/deployPage")
    public String deployPage() {
        return prefix + "deploy";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("system:bpmnInfo:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<BpmnInfo> query(BpmnInfoQueryObject qo) {

        return bpmnInfoService.query(qo);
    }

    //删除
    @RequiresPermissions("system:bpmnInfo:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResult delete(Long id) {
        bpmnInfoService.deleteBatch(id);
        return AjaxResult.success();
    }


    //处理上传上来的文件upload
    @RequiresPermissions("system:bpmnInfo:upload")
    @RequestMapping("/upload")
    @ResponseBody
    //接收到前端发送来的文件
    public AjaxResult upload(MultipartFile file) {
        //判空
        if (file != null && file.getSize() > 0) {//获取文件后缀名判断上传来的文件是否合法
            //获取文件名
            String originalFilename = file.getOriginalFilename();
            //获取后缀名
            String extension = FilenameUtils.getExtension(originalFilename);
//            if ("zip".endsWith(originalFilename) || "bpmn".endsWith(originalFilename)){
//
//            }
            if ("zip".equalsIgnoreCase(extension) || "bpmn".equalsIgnoreCase(extension)) {
                //返回文件的路径
                try {
                    //获取文件的路径,第一个参数是上级路径
                    //第二个参数是下级路径
                    String upload = FileUploadUtils.upload(SystemConfig.getUploadPath(), file);
                    return AjaxResult.success("上传成功", upload);
                } catch (Exception e) {
                    e.printStackTrace();
                    return AjaxResult.error("上传文件失败");
                }
            }else {
                return AjaxResult.error("只支出zip文件和bpmn文件");
            }
        } else {
            return AjaxResult.error("不允许上传空文件");
        }
    }


    //处理发送过来的文件和信息,去部署流程
    @RequestMapping("/deploy")
    @RequiresPermissions("system:bpmnInfo:deploy")
    @ResponseBody
    public AjaxResult deploy(String bpmnPath,String bpmnType,String info){
        String upload = bpmnInfoService.upload(bpmnPath, bpmnType, info);
        return AjaxResult.success("文件保存成功",upload);
    }


    //编辑edit
    @RequestMapping("/edit")
    @RequiresPermissions("system:bpmnInfo:edit")
    @ResponseBody
    public AjaxResult edit(BpmnInfo bpmnInfo){
       bpmnInfoService.update(bpmnInfo);
        return AjaxResult.success();
    }

    /**
     * 查看资源
     */
    @RequiresPermissions("business:bpmnInfo:readResource")
    @RequestMapping( "/readResource")
    @ResponseBody
    public void readResource(String deploymentId, String type , HttpServletResponse response) throws IOException {
        InputStream inputStream  =  bpmnInfoService.getResoucceByType(deploymentId, type);
        IOUtils.copy(inputStream,response.getOutputStream());
    }









}
